﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using Microsoft.Reporting.WebForms;
using System.Data;

namespace Catering.Catering_Page.JieGouJianKong
{
    public partial class MonitorView : System.Web.UI.Page
    {
        private static Catering_Rules.ViewOperaction viewOperaction = new Catering_Rules.ViewOperaction();
        private static View_Monitor vm = new View_Monitor();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string AREAID = UCDDLToolWithYearOfMonth1.GetDDL_Area();
            string CLASSID = UCDDLToolWithYearOfMonth1.GetDDL_Class();
            DateTime DTIME = UCDDLToolWithYearOfMonth1.GetDateTime();
            string TimeType = UCDDLToolWithYearOfMonth1.GetTimeType();
            if (DTIME.Year.Equals(1949))
            {
                return;
            }
            switch (TimeType)
            {
                case "Year":
                    GetXiaohaoSumByYear(AREAID, CLASSID, DTIME);
                    break;
                case "Month":
                    GetXiaohaoSumByMonth(AREAID, CLASSID, DTIME);
                    break;
            }
        }

        private void GetXiaohaoSumByYear(string AREAID, string CLASSID, DateTime DTIME)
        {
            string Title1 = "";
            string Title2 = "";
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            GetTitle(AREAID, CLASSID, DTIME, 0, out Title1, out Title2);
            DataTable dt = vm.GetViewSumToDataSet(pid,AREAID, CLASSID, DTIME, Title1, Title2);

            ReportViewer1.LocalReport.ReportPath = @"Catering_Page\Jiegoujiankong\Report\Xiaohao_Sum.rdlc";

            ReportDataSource rds = new ReportDataSource("DataSet1", dt);
            // 先清除报表数据源
            ReportViewer1.LocalReport.DataSources.Clear();
            // 绑定数据源
            ReportViewer1.LocalReport.DataSources.Add(rds);
            // 呈现出数据
            ReportViewer1.LocalReport.Refresh();

            ReportViewer1.Visible = true;
        }

        private void GetXiaohaoSumByMonth(string AREAID, string CLASSID, DateTime DTIME)
        {
            // 先清除报表数据源
            string Title1 = "";
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            string Title2 = "";
            GetTitle(AREAID, CLASSID, DTIME, 1, out Title1, out Title2);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.ReportPath = @"Catering_Page\Jiegoujiankong\Report\Xiaohao_SumByMonth.rdlc";
            DataTable dt = vm.GetViewSumToDataTableForMonth(pid,AREAID, CLASSID, DTIME, Title1);
            ReportDataSource rds = new ReportDataSource("DataSet1", dt);
            // 绑定数据源
            ReportViewer1.LocalReport.DataSources.Add(rds);
            // 呈现出数据
            ReportViewer1.LocalReport.Refresh();

            ReportViewer1.Visible = true;
        }

        public void GetTitle(string AREAID, string CLASSID, DateTime DTIME, int TimeType, out string title1, out string title2)
        {
            title1 = "";
            title2 = "";
            title1 = DTIME.Year + "年";
            if (TimeType == 1)//月
            {
                title1 += DTIME.Month + "月";
            }
            Catering_Model.OperationArea OAreaInfo = null;
            Catering_Model.ClassInfo ClassInfo = null;
            if (!string.IsNullOrEmpty(AREAID) && !AREAID.Equals("-10")) OAreaInfo = new Catering_Rules.OperationArea().GetModel(Convert.ToInt32(AREAID));
            if (!string.IsNullOrEmpty(CLASSID) && !CLASSID.Equals("-10")) ClassInfo = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(CLASSID));
            if (OAreaInfo != null && !string.IsNullOrEmpty(OAreaInfo.AreaName))
            {
                title1 += OAreaInfo.AreaName;
                if (ClassInfo != null && !string.IsNullOrEmpty(ClassInfo.CName))
                {
                    title1 += ClassInfo.CName + "班组";
                    title2 = ClassInfo.CName + "班组";
                }
                else
                {
                    title2 = OAreaInfo.AreaName;
                }
            }
            else
            {
                title2 = "所有平台";
            }
            title1 = string.IsNullOrEmpty(title1) ? "全部班组消耗分类汇总表" : title1 + "消耗分类汇总表";
        }
    }
}